import { useCallback, useState } from 'react';
export default () => {
  const [socket, setSocket] = useState(null); //socket链接
  const [userOnelineList, setUserOnelineList] = useState([]); // 在线用户
  const [msgList, setMsgList] = useState([]); //全局聊天记录

  const [titleList, setTitleList] = useState([]); // 定义主题

  const updateOnlineUsers = useCallback((userList) => {
    setUserOnelineList(userList);
  }, []);
  const updateMsgs = useCallback((msg) => {
    // debugger;
    // console.log(msgList, '----msg----');
    setMsgList(msg);
  }, []);
  const updateSocket = useCallback((ws) => {
    setSocket(ws);
  }, []);

  const updateTitles = useCallback((msg) => {
    setTitleList(msg);
  }, []);
  //   useEffect(() => {
  //     if (!ws.current) return;

  //     ws.current.onmessage = (e) => {
  //       if (isPaused) return;
  //       const message = JSON.parse(e.data);
  //       console.log('e', message);
  //     };
  //   }, [isPaused]);

  return {
    socket,
    userOnelineList,
    msgList,
    updateOnlineUsers,
    updateMsgs,
    updateSocket,
    titleList,
    updateTitles,
  };
};
